<?php

namespace App\Models\Trip\TripOrderRefund;

use Eloquent;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Validator;

/**
 * 无票退款明细,按照成本记录退款
 *
 * @property int                       $id
 * @property string                    $user_id   用户id
 * @property string                    $refund_sn 退款单号
 * @property string                    $order_sn  订单单号
 * @property int                       $type      退款项目类型 如:车票,保险费,购票手续费,坐席,票价差
 * @property string                    $name      费用名称
 * @property int                       $count     项目数量
 * @property string                    $amount    金额
 * @property Carbon|null               $created_at
 * @property Carbon|null               $updated_at
 * @property-read TripOrderRefund|null $refund
 * @method static Builder|NoTicketRefundDetail newModelQuery()
 * @method static Builder|NoTicketRefundDetail newQuery()
 * @method static Builder|NoTicketRefundDetail query()
 * @method static Builder|NoTicketRefundDetail whereAmount($value)
 * @method static Builder|NoTicketRefundDetail whereCount($value)
 * @method static Builder|NoTicketRefundDetail whereCreatedAt($value)
 * @method static Builder|NoTicketRefundDetail whereId($value)
 * @method static Builder|NoTicketRefundDetail whereName($value)
 * @method static Builder|NoTicketRefundDetail whereOrderSn($value)
 * @method static Builder|NoTicketRefundDetail whereRefundSn($value)
 * @method static Builder|NoTicketRefundDetail whereType($value)
 * @method static Builder|NoTicketRefundDetail whereUpdatedAt($value)
 * @method static Builder|NoTicketRefundDetail whereUserId($value)
 * @property string $out_trade_no 关联的支付单号
 * @method static Builder|NoTicketRefundDetail whereOutTradeNo($value)
 * @property int|null $trip_type 出行类型 1 火车 2 飞机 3 酒店
 * @method static Builder|NoTicketRefundDetail whereTripType($value)
 * @property string $origin_order_sn 改签原订单单号
 * @method static Builder|NoTicketRefundDetail whereOriginOrderSn($value)
 * @mixin Eloquent
 */
class NoTicketRefundDetail extends RefundDetail
{

    protected $table = 'trip_order_no_ticket_refund_details';
    protected $fillable
                     = [
            "user_id",
            "refund_sn",
            "order_sn",
            "out_trade_no",
            "origin_order_sn",
            "trip_type",        // 出行类型
            "type",
            "name",
            "count",
            "amount",
        ];

    /**
     *
     * @param TripOrderRefund $refund
     * @param array           $data
     *
     * @return bool
     * @throws Exception
     */

    public function addMore(TripOrderRefund $refund, array $data): bool
    {
        $validator = Validator::make($data, [
            "*.user_id"      => "required",
            "*.refund_sn"    => "required",
            "*.order_sn"     => "required",
            "*.out_trade_no" => "required",
            "*.type"         => "required",
            "*.name"         => "required",
            //			"*.count"     => "required",
            "*.amount"       => "required",
        ]);

        if ($validator->fails()) {
            throw new Exception("退款参数错误".$validator->errors()->first());
        }

        foreach ($data as $key => $detail) {
            $data[$key]['count']      = $detail['count'] ?? 1;
            $data[$key]['created_at'] = $refund->getCreatedAt();
            $data[$key]['updated_at'] = $refund->getCreatedAt();
        }

        return $this->insert($data);
    }

    public function findOneByDetail(array $detail): ?self
    {
        return $this->findOneByOrderTypeName($detail['order_sn'], $detail['type'], $detail['name'], $detail['out_trade_no']);
    }

    public function findOneByOrderTypeName(string $order_sn, int $type, string $name, string $pay_sn = ''): ?self
    {
        $where = [
            'order_sn' => $order_sn,
            'type'     => $type,
            'name'     => $name,
        ];

        if ($type == self::TASK_EDIT_DIFF_FEE_REFUND) {
            $where["out_trade_no"] = $pay_sn;
        }

        return $this->where($where)->first();
    }

    public function addOne(TripOrderRefund $refund, array $data)
    {
        // TODO: Implement addOne() method.
    }
}